package com.dingyh.shopwebbackground.controller;


import com.dingyh.shopwebbackground.entity.*;
import com.dingyh.shopwebbackground.mapper.CouponsMapper;
import com.dingyh.shopwebbackground.service.CouponsService;
import com.dingyh.shopwebbackground.util.CommonResult;
import com.dingyh.shopwebbackground.util.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dingyuhan
 * @since 2023-02-22
 */
@RestController
@CrossOrigin
@RequestMapping("/coupons")
public class CouponsController {

    @Autowired
    CouponsService couponsService;


    @RequestMapping("/getAllCoupons")
    public CommonResult<Object> getAllCoupons(@RequestParam(defaultValue = "1",required = true)int pageIndex, @RequestParam(defaultValue = "10",required = false)int pageSize){
        PageHelper.offsetPage((pageIndex - 1) * pageSize,pageSize);
        List<Coupons> couponsList = couponsService.getAllCoupons();
        long currTime = System.currentTimeMillis();
        couponsList.forEach(coupons -> {
            String ifdelete = "0";
            if(Long.compare(currTime, coupons.getTimeEnd()) != -1){
                ifdelete = "1";
            }else if (Long.compare(coupons.getTimeStart(),currTime)!=-1){
                ifdelete = "2";
            }
            coupons.setIfDelete(ifdelete);
        });
        PageInfo<Coupons> pageInfo = new PageInfo<>(couponsList);
        return new CommonResult<>(200,"",pageInfo);
    }

    @RequestMapping("/selectById")
    public CommonResult<Object> selectById(String id,
                                           @RequestParam(defaultValue = "1",required = true )int pageIndex, @RequestParam(defaultValue = "10",required = true )int pageSize){
        PageHelper.startPage(pageIndex, pageSize);//初始化分页
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        List<Coupons> couponsList = couponsService.selectByIdCoupons(map);
        PageInfo<Coupons> page = new PageInfo<>(couponsList);
        if (!page.getList().isEmpty()) {
            return new CommonResult(200,"",page);
        }else {
            return new CommonResult(500,"该优惠券不存在！",null);
        }
    }

    @RequestMapping("/selectByName")
    public CommonResult<Object> selectByName(String name,
                                           @RequestParam(defaultValue = "1",required = true )int pageIndex, @RequestParam(defaultValue = "10",required = true )int pageSize){
        PageHelper.startPage(pageIndex, pageSize);//初始化分页
        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        List<Coupons> couponsList = couponsService.selectByName(map);
        PageInfo<Coupons> page = new PageInfo<>(couponsList);
        if (!page.getList().isEmpty()) {
            return new CommonResult(200,"",page);
        }else {
            return new CommonResult(500,"该优惠券不存在！",null);
        }
    }

    @RequestMapping("/removeCoupons")
    public CommonResult<Object> removeCoupons(Coupons coupons){
        if (couponsService.removeCoupons(coupons)){
            return new CommonResult(200, "下架成功！", null);
        }else{
            return new CommonResult(500, "下架失败！", null);
        }
    };

    @RequestMapping("/addCoupons")
    public CommonResult<Object> addCoupons(String name, String efforts, Long timeStart,Long timeEnd){
        Coupons coupons = Coupons.builder()
                .id(UUIDUtil.generator())
                .name(name)
                .efforts(efforts)
                .timeStart(timeStart)
                .timeEnd(timeEnd)
                .ifDelete("0")
                .build();
        boolean result = couponsService.save(coupons);
        if (result) {
            return new CommonResult(200, "新建优惠券成功！", null);
        } else {
            return new CommonResult(500, "新建优惠券失败！", null);
        }
    }
}

